package com.vhall.component.service.data.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.data.AccountForDataMapper;
import com.vhall.component.dao.data.AdminForDataMapper;
import com.vhall.component.dao.room.RoomJoinsMapper;
import com.vhall.component.entity.account.AccountEntity;
import com.vhall.component.entity.account.AdminsEntity;
import com.vhall.component.entity.account.dto.IndexDto;
import com.vhall.component.entity.data.enums.SelectTimeIntervalOptionsEnum;
import com.vhall.component.entity.data.vo.*;
import com.vhall.component.entity.interactive.vo.QuestionNumReqVO;
import com.vhall.component.entity.interactive.vo.QuestionNumRspVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.entity.room.entity.RoomJoinsEntity;
import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.framework.common.utils.CollectionUtils;
import com.vhall.component.plugin.common.constant.ConstStr;
import com.vhall.component.plugin.common.constant.RoomConstant;
import com.vhall.component.service.data.DataAdminService;
import com.vhall.component.service.interactive.DocumentsInnerService;
import com.vhall.component.service.interactive.QuestionService;
import com.vhall.component.service.room.RoomInnerService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author guoxin.wang
 * @date 2021-07-29 10:37
 */
@Slf4j
@Service
public class DataAdminServiceImpl implements DataAdminService {

    private static final String ADMIN = "admin";
    private static final String EXPORT = "export";
    private static final String CREATED_AT = "created_at";

    @Autowired
    private AccountForDataMapper accountForDataMapper;

    @Autowired
    private RoomInnerService roomInnerService;

    @Autowired
    private DocumentsInnerService documentsInnerService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private AdminForDataMapper adminForDataMapper;

    @Autowired
    private RoomJoinsMapper roomJoinsMapper;

    @Override
    public AdminLiveRspVO live(AdminLiveReqVO reqVO) {
        AdminLiveRspVO result = new AdminLiveRspVO();
        if (reqVO.getStatus() == RoomConstant.LIVE_STATUS_START) {
            // 直播中数据统计
            result = liveData(reqVO.getIlId(), reqVO.getBeginTime(), reqVO.getEndTime(), ADMIN);
        } else if (reqVO.getStatus() == RoomConstant.LIVE_PLAY_BACK) {
            // 回访数据统计
            result = recordData(reqVO.getIlId(), reqVO.getBeginTime(), reqVO.getEndTime(), ADMIN);
        } else if (reqVO.getStatus() == RoomConstant.LIVE_PLAY_ALL) {
            // 全部
            result = allData(reqVO.getIlId(), reqVO.getBeginTime(), reqVO.getEndTime(), ADMIN);
        }
        // 直播间聊天消息数
        Integer messageTotal = liveMessageTotal(reqVO.getIlId(), reqVO.getBeginTime(), reqVO.getEndTime());
        HashMap<String, Object> liveMessage = new HashMap<>(4);
        liveMessage.put("stat", messageTotal);
        liveMessage.put("unit", "条");
        liveMessage.put(EXPORT, "live_message");
        result.setLiveMessage(liveMessage);

        // 问卷统计
        QuestionNumRspVO vo = answerQuestionTotal(reqVO.getIlId(), reqVO.getBeginTime(), reqVO.getEndTime());
        Integer answer = vo.getByAccountNum() == null ? 0 : vo.getByAccountNum();
        Integer question = vo.getByRoomNum() == null ? 0 : vo.getByRoomNum();
        Map<String, Object> liveQuestion = new HashMap<>(4);
        liveQuestion.put("stat", question);
        liveQuestion.put("unit", "份");
        liveQuestion.put(EXPORT, "");
        result.setLiveQuestion(liveQuestion);
        Map<String, Object> liveQuestionAnswer = new HashMap<>(4);
        liveQuestionAnswer.put("stat", answer);
        liveQuestionAnswer.put("unit", "份");
        liveQuestionAnswer.put(EXPORT, "");
        result.setLiveQuestionAnswer(liveQuestionAnswer);
        return result;
    }

    private QuestionNumRspVO answerQuestionTotal(Integer ilId, LocalDateTime beginTime, LocalDateTime endTime) {
        QuestionNumReqVO reqVO = new QuestionNumReqVO();
        if (ilId != null) {
            RoomsEntityDTO roomInfo = roomInnerService.getRoomInfoByIlId(ilId.toString());
            if (roomInfo != null) {
                reqVO.setRoomId(roomInfo.getRoomId());
            }
        }
        reqVO.setBeginDate(LocalDateTimeUtil.formatNormal(beginTime));
        reqVO.setEndDate(LocalDateTimeUtil.formatNormal(endTime));
        return questionService.statNum(reqVO);
    }

    private Integer liveMessageTotal(Integer ilId, LocalDateTime beginTime, LocalDateTime endTime) {
        if (ilId == null) {
            ilId = 0;
        }
        return roomInnerService.sumMessageTotal(ilId, beginTime, endTime);
    }


    @Override
    public HashMap<String, IndexDto> index() {
        String time = DateUtil.format(new Date(), CommonConstant.DATE_FORMAT);
        String beginTime = time + " " + ConstStr.TIME_ZERO;
        return indexPlatform(beginTime);
    }




    @NotNull
    private HashMap<String, IndexDto> indexPlatform(String beginTime) {
        HashMap<String, IndexDto> data = new HashMap<>(8);
        IndexDto liveStat = roomInnerService.getCount(beginTime);
        IndexDto documentStat = documentsInnerService.getCount(beginTime);
        IndexDto questionStat = questionService.getCount(beginTime);
        IndexDto adminStat = new IndexDto();
        IndexDto accountStat = new IndexDto();
        data.put("live_stat", liveStat);
        data.put("account_stat", accountStat);
        data.put("admin_stat", adminStat);
        data.put("question_stat", questionStat);
        data.put("document_stat", documentStat);

        // 累计观看次数
        IndexDto accumulatedViewsCount = getAccumulatedViewsCount(beginTime);
        data.put("accumulated_views", accumulatedViewsCount);

        // 累计观看人数
        IndexDto accumulatedNumberOfViewersCount = getAccumulatedNumberOfViewersCount(beginTime);
        data.put("accumulated_number_of_viewers", accumulatedNumberOfViewersCount);

        return data;
    }

    /**
     * 累计观看人数-组织架构
     * @param beginTime
     * @param orgCode
     * @return
     */
    private IndexDto getAccumulatedNumberOfViewersCount(String beginTime,String orgCode) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT);
        LocalDateTime time = LocalDateTime.parse(beginTime, df);
        LocalDateTime today = LocalDate.now().atStartOfDay();

        Integer total = accountForDataMapper.selectWatchUVCount(null,null);

        Integer day = accountForDataMapper.selectWatchUVCount(time,null);

        Integer week = accountForDataMapper.selectWatchUVCount(time.plusDays(-1 * SelectTimeIntervalOptionsEnum.PAST_7_DAYS.getDay()),today);

        Integer month = accountForDataMapper.selectWatchUVCount(time.plusDays(-1 * SelectTimeIntervalOptionsEnum.PAST_30_DAYS.getDay()),today);

        Integer year = accountForDataMapper.selectWatchUVCount(time.minusYears(1L),today);
        IndexDto indexDto = new IndexDto();
        indexDto.setTotal(total);
        indexDto.setDay(day);
        indexDto.setWeek(week);
        indexDto.setMonth(month);
        indexDto.setYear(year);
        return indexDto;
    }

    /**
     * 累计观看人数
     * @param beginTime
     * @return
     */
    private IndexDto getAccumulatedNumberOfViewersCount(String beginTime) {
        return getAccumulatedNumberOfViewersCount(beginTime,null);
    }

    /**
     * 累计观看次数-组织架构限定
     * @param beginTime
     * @param orgCode
     * @return
     */
    public IndexDto getAccumulatedViewsCount(String beginTime,String orgCode) {

        UserParamVO reqVO = new UserParamVO();
        reqVO.setStartTime(null);

        int pvCount = getPvCount(reqVO);

        Integer total = pvCount;


        String todayStartTime = DateUtil.format(LocalDateTime.now(), DatePattern.NORM_DATE_PATTERN) + " " + ConstStr.TIME_ZERO;
        String yesterdayStr = DateUtil.format(LocalDateTime.now().plusDays(-1), DatePattern.NORM_DATE_PATTERN) + " " + ConstStr.TIME_ZERO;

        reqVO.setStartTime(todayStartTime);
        Integer day = getPvCount(reqVO);


        String lastWeekStartTime = DateUtil.format(LocalDateTime.now().plusDays(-1 * SelectTimeIntervalOptionsEnum.PAST_7_DAYS.getDay()), DatePattern.NORM_DATE_PATTERN) + " " + ConstStr.TIME_ZERO;
        reqVO.setStartTime(lastWeekStartTime);
        reqVO.setEndTime(yesterdayStr);
        Integer week = getPvCount(reqVO);

        String lastMonthStartTime = DateUtil.format(LocalDateTime.now().plusDays(-1 * SelectTimeIntervalOptionsEnum.PAST_30_DAYS.getDay()), DatePattern.NORM_DATE_PATTERN) + " " + ConstStr.TIME_ZERO;
        reqVO.setStartTime(lastMonthStartTime);
        reqVO.setEndTime(yesterdayStr);
        Integer month = getPvCount(reqVO);

        String yearStartTime = DateUtil.format(DateUtil.offset(new DateTime(), DateField.YEAR, -1).toLocalDateTime(), DatePattern.NORM_DATE_PATTERN) + " " + ConstStr.TIME_ZERO;
        reqVO.setStartTime(yearStartTime);
        reqVO.setEndTime(yesterdayStr);
        Integer year = getPvCount(reqVO);
        IndexDto indexDto = new IndexDto();
        indexDto.setTotal(total);
        indexDto.setDay(day);
        indexDto.setWeek(week);
        indexDto.setMonth(month);
        indexDto.setYear(year);
        return indexDto;
    }

    /**
     * 获取累计观看次数
     * @param beginTime
     * @return
     */
    public IndexDto getAccumulatedViewsCount(String beginTime) {
        return getAccumulatedViewsCount(beginTime,null);
    }

    private int getPvCount(UserParamVO reqVO) {

        UserBasicStatisticsRspVO ubsrv = accountForDataMapper.getBasicStatisticsInfo(reqVO);
        int pvCount = (ubsrv.getPvCount() != null && ubsrv.getPvCount() > 0) ? ubsrv.getPvCount() : 0;

        UserBasicStatisticsRspVO ubsrvVod = accountForDataMapper.getVodBasicStatisticsInfo(reqVO);
        pvCount += (ubsrvVod.getPvCount() != null && ubsrvVod.getPvCount() > 0) ? ubsrvVod.getPvCount() : 0;
        return pvCount;
    }
    private IndexDto getAdminsCount(String beginTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT);
        LocalDateTime time = LocalDateTime.parse(beginTime, df);
        QueryWrapper<AdminsEntity> queryWrapper = new QueryWrapper<>();
        Integer total = adminForDataMapper.selectCount(queryWrapper);
        queryWrapper.clear();
        Integer day = adminForDataMapper.selectCount(queryWrapper.ge(CREATED_AT, time));
        queryWrapper.clear();
        Integer week = adminForDataMapper.selectCount(queryWrapper.ge(CREATED_AT, time.minusWeeks(1L)));
        queryWrapper.clear();
        Integer month = adminForDataMapper.selectCount(queryWrapper.ge(CREATED_AT, time.minusMonths(1L)));
        queryWrapper.clear();
        Integer year = adminForDataMapper.selectCount(queryWrapper.ge(CREATED_AT, time.minusYears(1L)));
        IndexDto indexDto = new IndexDto();
        indexDto.setTotal(total);
        indexDto.setDay(day);
        indexDto.setWeek(week);
        indexDto.setMonth(month);
        indexDto.setYear(year);
        return indexDto;
    }

    private IndexDto getAccountCount(String beginTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT);
        LocalDateTime time = LocalDateTime.parse(beginTime, df);
        LambdaQueryWrapper<AccountEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AccountEntity::getAccountType, Arrays.asList(1, 2));
        Integer total = accountForDataMapper.selectCount(wrapper);
        Integer day = accountForDataMapper.selectCount(wrapper.ge(AccountEntity::getCreatedAt, time));
        wrapper.clear();
        wrapper.in(AccountEntity::getAccountType, Arrays.asList(1, 2));
        Integer week = accountForDataMapper.selectCount(wrapper.ge(AccountEntity::getCreatedAt, time.minusWeeks(1L)));
        wrapper.clear();
        wrapper.in(AccountEntity::getAccountType, Arrays.asList(1, 2));
        Integer month = accountForDataMapper.selectCount(wrapper.ge(AccountEntity::getCreatedAt, time.minusMonths(1L)));
        wrapper.clear();
        wrapper.in(AccountEntity::getAccountType, Arrays.asList(1, 2));
        Integer year = accountForDataMapper.selectCount(wrapper.ge(AccountEntity::getCreatedAt, time.minusYears(1L)));
        IndexDto indexDto = new IndexDto();
        indexDto.setTotal(total);
        indexDto.setDay(day);
        indexDto.setWeek(week);
        indexDto.setMonth(month);
        indexDto.setYear(year);
        return indexDto;
    }

    /**
     * 获取全部统计信息
     *
     * @param ilId      房间id
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param accountId 用户id
     * @return AdminLiveRspVO
     */
    private AdminLiveRspVO allData(String accountId, Integer ilId, LocalDateTime beginTime, LocalDateTime endTime, String source) {
        AdminLiveRspVO result = new AdminLiveRspVO();
        UserParamVO param = new UserParamVO();
        param.setAccountId(accountId);
        param.setIlId(ilId);
        param.setStartTime(DateUtil.format(beginTime, DatePattern.NORM_DATETIME_PATTERN));
        param.setEndTime(DateUtil.format(endTime, DatePattern.NORM_DATETIME_PATTERN));

        // 1、直播数据
        UserBasicStatisticsRspVO ubsrvOfBasic = accountForDataMapper.getBasicStatisticsInfo(param);
        int uvStat = ubsrvOfBasic == null ? 0 : ubsrvOfBasic.getUvCount() == null ? 0 : ubsrvOfBasic.getUvCount();
        Integer pvStat = ubsrvOfBasic == null ? 0 : ubsrvOfBasic.getPvCount() == null ? 0 : ubsrvOfBasic.getPvCount();
        Integer totalTime = ubsrvOfBasic == null ? 0 : ubsrvOfBasic.getTotalDuration() == null ? 0 : ubsrvOfBasic.getTotalDuration();
        Integer avgWatchTimeResult = uvStat > 0 ? Convert.toInt(Math.ceil(totalTime.doubleValue() / (double) uvStat)) : 0;

        // liveUv 累计观众人数
        Map<String, Object> liveUv = new HashMap<>(4);
        liveUv.put("stat", uvStat);
        liveUv.put("unit", "人");
        liveUv.put(EXPORT, ilId == null ? "" : "uv");
        result.setLiveUv(liveUv);

        // livePv 累计观看次数
        Map<String, Object> livePv = new HashMap<>(4);
        livePv.put("stat", pvStat);
        livePv.put("unit", "次");
        livePv.put(EXPORT, "pv");
        result.setLivePv(livePv);

        // maxConcurrent 最高并发数
        final Integer userMaxPv = accountForDataMapper.getUserMaxPv(param);
        Map<String, Object> maxConcurrent = new HashMap<>(4);
        maxConcurrent.put("stat", userMaxPv);
        maxConcurrent.put("unit", "人");
        maxConcurrent.put(EXPORT, "");
        result.setMaxConcurrent(maxConcurrent);

        // 观看时长
        Map<String, Object> watchTime = new HashMap<>(4);
        watchTime.put("stat", totalTime);
        watchTime.put("unit", "分钟");
        watchTime.put(EXPORT, "");
        result.setWatchTime(watchTime);

        if (ilId != null) {
            // 直播消耗流量
            BigDecimal liveFlowResult = accountForDataMapper.getUserLiveFlow(param);
            Map<String, Object> liveFlow = new HashMap<>(4);
            liveFlow.put("stat", liveFlowResult == null ? new BigDecimal("0") : liveFlowResult);
            liveFlow.put("unit", "MB");
            liveFlow.put(EXPORT, "");
            result.setLiveFlow(liveFlow);
        }

        // 2、点播数据
        UserBasicStatisticsRspVO ubsrvOfRecord = accountForDataMapper.getVodBasicStatisticsInfo(param);
        int uvStatOfRecord = ubsrvOfRecord == null ? 0 : ubsrvOfRecord.getUvCount() == null ? 0 : ubsrvOfRecord.getUvCount();
        Integer pvStatOfRecord = ubsrvOfRecord == null ? 0 : ubsrvOfRecord.getPvCount() == null ? 0 : ubsrvOfRecord.getPvCount();
        Integer totalTimeOfRecord = ubsrvOfRecord == null ? 0 : ubsrvOfRecord.getTotalDuration() == null ? 0 : ubsrvOfRecord.getTotalDuration();
        // liveUv 累计观众人数
        int uvStatNew = (Integer) result.getLiveUv().get("stat") + uvStatOfRecord;
        result.getLiveUv().put("stat", uvStatNew);
        // livePv 累计观看次数
        Integer pvStatNew = (Integer) result.getLivePv().get("stat") + pvStatOfRecord;
        result.getLivePv().put("stat", pvStatNew);
        if (ilId != null) {
            // 点播消耗流量
            BigDecimal vodFlow = accountForDataMapper.getUserVodFlow(param);
            if (vodFlow == null) {
                vodFlow = new BigDecimal("0");
            }
            BigDecimal vodFlowNew = vodFlow.add((BigDecimal) result.getLiveFlow().get("stat"));
            result.getLiveFlow().put("stat", vodFlowNew);

            // 回放存储空间
            BigDecimal recordStorageResult = accountForDataMapper.getUserVodStorage(param);
            Map<String, Object> recordStorage = new HashMap<>(4);
            recordStorage.put("stat", recordStorageResult == null ? 0 : recordStorageResult);
            recordStorage.put("unit", "MB");
            recordStorage.put(EXPORT, "");
            result.setRecordStorage(recordStorage);
        }
        // 观看时长 += 点播观看时长
        Integer watchTimeNew = (Integer) result.getWatchTime().get("stat") + totalTimeOfRecord;
        result.getWatchTime().put("stat", watchTimeNew);

        // 人均观看时长
        Map<String, Object> avgWatchTime = new HashMap<>(4);
        avgWatchTime.put("stat", uvStatNew > 0 ? Math.ceil(watchTimeNew.doubleValue() / uvStatNew) : 0);
        avgWatchTime.put("unit", "分钟");
        avgWatchTime.put(EXPORT, "");
        result.setAvgWatchTime(avgWatchTime);

        if (ilId != null) {
            // 人均观看次数
            Map<String, Object> avgLivePv = new HashMap<>(4);
            avgLivePv.put("stat", uvStatNew > 0 ? Math.ceil(pvStatNew.doubleValue() / uvStatNew) : 0);
            avgLivePv.put("unit", "次");
            avgLivePv.put(EXPORT, "");
            result.setAvgLivePv(avgLivePv);
        }

        return result;
    }

    /**
     * 获取点播统计信息
     *
     * @param accountId 用户id
     * @param ilId      互动房间ID值
     * @param beginTime 开始时间
     * @param endTime   截止时间
     * @return AdminLiveRspVO
     */
    private AdminLiveRspVO recordData(String accountId, Integer ilId, LocalDateTime beginTime, LocalDateTime endTime, String source) {
        AdminLiveRspVO result = new AdminLiveRspVO();
        UserParamVO param = new UserParamVO();
        param.setAccountId(accountId);
        param.setIlId(ilId);
        param.setStartTime(DateUtil.format(beginTime, DatePattern.NORM_DATETIME_PATTERN));
        param.setEndTime(DateUtil.format(endTime, DatePattern.NORM_DATETIME_PATTERN));
        UserBasicStatisticsRspVO ubsrv = accountForDataMapper.getVodBasicStatisticsInfo(param);
        int uvStat = ubsrv == null ? 0 : ubsrv.getUvCount() == null ? 0 : ubsrv.getUvCount();
        Integer pvStat = ubsrv == null ? 0 : ubsrv.getPvCount() == null ? 0 : ubsrv.getPvCount();
        Integer totalTime = ubsrv == null ? 0 : ubsrv.getTotalDuration() == null ? 0 : ubsrv.getTotalDuration();
        Integer avgWatchTimeResult = uvStat > 0 ? Convert.toInt(Math.ceil(totalTime.doubleValue() / uvStat)) : 0;
        // 回放时长， 直播生成的回放视频总时长
        Integer videoTotalTime = accountForDataMapper.getVodTotalTime(param);

        // liveUv 累计观众人数
        Map<String, Object> liveUv = new HashMap<>(4);
        liveUv.put("stat", uvStat);
        liveUv.put("unit", "人");
        liveUv.put(EXPORT, "uv");
        result.setLiveUv(liveUv);

        // livePv 累计观看次数
        Map<String, Object> livePv = new HashMap<>(4);
        livePv.put("stat", pvStat);
        livePv.put("unit", "次");
        livePv.put(EXPORT, "pv");
        result.setLivePv(livePv);

        // 观看时长
        Map<String, Object> watchTime = new HashMap<>(4);
        watchTime.put("stat", totalTime);
        watchTime.put("unit", "分钟");
        watchTime.put(EXPORT, "");
        result.setWatchTime(watchTime);

        // 平均观看时长
        Map<String, Object> avgWatchTime = new HashMap<>(4);
        avgWatchTime.put("stat", avgWatchTimeResult);
        avgWatchTime.put("unit", "分钟");
        avgWatchTime.put(EXPORT, "");
        result.setAvgWatchTime(avgWatchTime);

        // 回放时长， 直播生成的回放视频总时长
        Map<String, Object> videoTime = new HashMap<>(4);
        videoTime.put("stat", videoTotalTime == null ? 0 : videoTotalTime);
        videoTime.put("unit", "分钟");
        videoTime.put(EXPORT, "");
        result.setVideoTime(videoTime);

        return result;
    }

    /**
     * 直播汇总数据
     *
     * @param ilId      房间id
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param accountId 用户id
     * @param source    /console /admin
     * @return AdminLiveRspVO 返回值
     */
    private AdminLiveRspVO liveData(String accountId, Integer ilId, LocalDateTime beginTime, LocalDateTime endTime, String source) {
        AdminLiveRspVO result = new AdminLiveRspVO();

        UserParamVO param = new UserParamVO();
        param.setAccountId(accountId);
        param.setIlId(ilId);
        param.setStartTime(DateUtil.format(beginTime, DatePattern.NORM_DATETIME_PATTERN));
        param.setEndTime(DateUtil.format(endTime, DatePattern.NORM_DATETIME_PATTERN));
        UserBasicStatisticsRspVO ubsrv = accountForDataMapper.getBasicStatisticsInfo(param);

        int uvStat = ubsrv == null ? 0 : ubsrv.getUvCount() == null ? 0 : ubsrv.getUvCount();
        Integer pvStat = ubsrv == null ? 0 : ubsrv.getPvCount() == null ? 0 : ubsrv.getPvCount();
        Integer totalTime = ubsrv == null ? 0 : ubsrv.getTotalDuration() == null ? 0 : ubsrv.getTotalDuration();
        Integer avgWatchTimeResult = uvStat > 0 ? Convert.toInt(Math.ceil(totalTime.doubleValue() / uvStat)) : 0;

        // liveUv 累计观众人数
        Map<String, Object> liveUv = new HashMap<>(4);
        liveUv.put("stat", uvStat);
        liveUv.put("unit", "人");
        liveUv.put(EXPORT, "uv");
        result.setLiveUv(liveUv);

        // livePv 累计观看次数
        Map<String, Object> livePv = new HashMap<>(4);
        livePv.put("stat", pvStat);
        livePv.put("unit", "次");
        livePv.put(EXPORT, "pv");
        result.setLivePv(livePv);

        // liveDuration 直播时长
        Integer liveDurationResult = accountForDataMapper.liveDuration(param);
        Map<String, Object> liveDuration = new HashMap<>(4);
        liveDuration.put("stat", liveDurationResult == null ? 0 : liveDurationResult);
        liveDuration.put("unit", "分钟");
        liveDuration.put(EXPORT, "");
        result.setLivingDuration(liveDuration);

        // 平均观看时长
        Map<String, Object> avgWatchTime = new HashMap<>(4);
        avgWatchTime.put("stat", avgWatchTimeResult);
        avgWatchTime.put("unit", "分钟");
        avgWatchTime.put(EXPORT, "");
        result.setAvgWatchTime(avgWatchTime);

        // 观看时长
        Map<String, Object> watchTime = new HashMap<>(4);
        watchTime.put("stat", totalTime);
        watchTime.put("unit", "分钟");
        watchTime.put(EXPORT, "");
        result.setWatchTime(watchTime);

        // maxConcurrent 最高并发数
        List<UserWatchPvRspVO> maxConcurrentResult = accountForDataMapper.getUserWatchPvInfo(param);
        Map<String, Object> maxConcurrent = new HashMap<>(4);
        maxConcurrent.put("stat", maxConcurrentResult == null || CollectionUtils.isEmpty(maxConcurrentResult) ? 0 : maxConcurrentResult.get(0) == null ? 0 : maxConcurrentResult.get(0).getWatchCount());
        maxConcurrent.put("unit", "人");
        maxConcurrent.put(EXPORT, "");
        result.setMaxConcurrent(maxConcurrent);

        // 人均观看次数
        Map<String, Object> avgLivePv = new HashMap<>(4);
        avgLivePv.put("stat", uvStat > 0 ? Math.ceil(pvStat.doubleValue() / uvStat) : 0);
        avgLivePv.put("unit", "次");
        avgLivePv.put(EXPORT, "");
        result.setAvgLivePv(avgLivePv);

        return result;
    }

    private AdminLiveRspVO allData(Integer ilId, LocalDateTime beginTime, LocalDateTime endTime, String source) {
        final RoomsEntityDTO roomInfoByIlId = roomInnerService.getRoomInfoByIlId(ilId.toString());
        return allData(roomInfoByIlId.getAccountId().toString(), ilId, beginTime, endTime, source);
    }

    /**
     * 获取点播统计信息
     *
     * @param ilId      互动房间ID值
     * @param beginTime 开始时间
     * @param endTime   截止时间
     * @return AdminLiveRspVO
     */
    private AdminLiveRspVO recordData(Integer ilId, LocalDateTime beginTime, LocalDateTime endTime, String source) {
        return recordData(null, ilId, beginTime, endTime, source);
    }

    /**
     * 直播汇总数据
     *
     * @param ilId      房间id
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param source    /console /admin
     * @return AdminLiveRspVO 返回值
     */
    private AdminLiveRspVO liveData(Integer ilId, LocalDateTime beginTime, LocalDateTime endTime, String source) {
        return liveData(null, ilId, beginTime, endTime, source);
    }
}